//
//  ViewController.m
//  09_tableView_省市区2
//
//  Created by Mars_ZXB on 16/7/26.
//  Copyright © 2016年 BAT3. All rights reserved.
//

#import "ViewController.h"
#import "Province.h"
#import "DistricViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *allProvinceArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"省份-市区";
    [self createData];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}
/**
 *  存放数据
 */
- (void)createData {
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09_tableView_省市区2/09_tableView_省市区2/area.txt";
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    self.allProvinceArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *province = [[Province alloc] initWithName:area];
            [_allProvinceArray addObject:province];
        } else if (![area hasPrefix:@"   "]) {
            City *city = [[City alloc] initWithName:area];
            Province *province = [_allProvinceArray lastObject];
            [province.arr addObject:city];
        } else {
            Distric *distric = [[Distric alloc] initWithName:area];
            Province *province = [_allProvinceArray lastObject];
            City *city = [province.arr lastObject];
            [city.arr addObject:distric];
        }
    }
}
//必须实现的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _allProvinceArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_allProvinceArray[section] arr].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuse"];
    }
    Province *pro = _allProvinceArray[indexPath.section];
    City *city = pro.arr[indexPath.row];
    NSArray *nameArr = [city.name componentsSeparatedByString:@" "];
    cell.textLabel.text = nameArr[2];
    cell.detailTextLabel.text = nameArr.lastObject;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[_allProvinceArray[section] name] componentsSeparatedByString:@" "].firstObject;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DistricViewController *disVC = [[DistricViewController alloc] init];
    Province *pro = _allProvinceArray[indexPath.section];
    disVC.city = pro.arr[indexPath.row];
    [self.navigationController pushViewController:disVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
